﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TGDEXG : UIGAAUF
    {
        #region ==dwReq=========== 
        public static readonly DependencyProperty dwReqProperty = DependencyProperty.Register("dwReq",
        typeof(DataRow), typeof(TGDEXG),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwReqChanged));

        public DataRow dwReq
        {
            get
            {
                return (DataRow)GetValue(dwReqProperty);
            }
            set
            {
                SetValue(dwReqProperty, value);
                LOADBYREQ();
            }
        }
        private static void OdwReqChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGDEXG;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwReq = row;
            }
        }
        #endregion
        #region ==ERLKZ===========
        public static readonly DependencyProperty ERLKZProperty = DependencyProperty.Register("ERLKZ",
            typeof(bool), typeof(TGDEXG),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnERLKZChanged));
        public bool ERLKZ
        {
            get
            {
                return (bool)GetValue(ERLKZProperty);
            }
            set
            {
                SetValue(ERLKZProperty, value);
            }
        }
        private static void OnERLKZChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGDEXG;
            if (src != null)
            {
                src.ERLKZ = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==ABPSTKLG===========
        public static readonly DependencyProperty ABPSTKLGProperty = DependencyProperty.Register("ABPSTKLG",
        typeof(double), typeof(TGDEXG),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnABPSTKLGChanged));
        public double ABPSTKLG
        {
            get
            {
                return (double)GetValue(ABPSTKLGProperty);
            }
            set
            {
                SetValue(ABPSTKLGProperty, value);
                OnABPSTKLGChg();
            }
        }
        private static void OnABPSTKLGChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGDEXG;
            if (src != null)
            {
                src.ABPSTKLG = (double)e.NewValue;
            }
        }
        public virtual void OnABPSTKLGChg()
        {
            if (dwMaster == null)
                return;

        }
        #endregion 
        #region ==ABPSOLLMENGE5===========
        public static readonly DependencyProperty ABPSOLLMENGE5Property = DependencyProperty.Register("ABPSOLLMENGE5",
        typeof(double), typeof(TGDEXG),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnABPSOLLMENGE5Changed));
        public double ABPSOLLMENGE5
        {
            get
            {
                return (double)GetValue(ABPSOLLMENGE5Property);
            }
            set
            {
                SetValue(ABPSOLLMENGE5Property, value);
                OnABPSOLLMENGE5Chg();
            }
        }
        private static void OnABPSOLLMENGE5Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGDEXG;
            if (src != null)
            {
                src.ABPSOLLMENGE5 = (double)e.NewValue;
            }
        }
        public virtual void OnABPSOLLMENGE5Chg()
        {
            if (dwMaster == null)
                return;

        }
        #endregion 
        #region ==ABPSOLLMENGE7===========
        public static readonly DependencyProperty ABPSOLLMENGE7Property = DependencyProperty.Register("ABPSOLLMENGE7",
        typeof(double), typeof(TGDEXG),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnABPSOLLMENGE7Changed));
        public double ABPSOLLMENGE7
        {
            get
            {
                return (double)GetValue(ABPSOLLMENGE7Property);
            }
            set
            {
                SetValue(ABPSOLLMENGE7Property, value);
                OnABPSOLLMENGE7Chg();
            }
        }
        private static void OnABPSOLLMENGE7Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TGDEXG;
            if (src != null)
            {
                src.ABPSOLLMENGE7 = (double)e.NewValue;
            }
        }
        public virtual void OnABPSOLLMENGE7Chg()
        {
            if (dwMaster == null)
                return;

        }
        #endregion


        public TGDEXG()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGAAUF();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            ERLKZ = true;
            if (dwMaster == null)
            {
                ERLKZ = false;
                return;
            }
        }
        bool IsFP = false;
        public void LOADBYREQ()
        {
            try
            {

                if (dwReq == null)
                {
                    return;
                }
                IsFP = false;
                PB.IsExpanded = false;
                dwData = TMV.VMU.TBMain.NewRow();
                dwData["ABAMANDANT"] = app.Mandant;
                dwData["ABAAUFKRS"] = dwReq["ABPAUFKRS"];
                dwData["ABAAUFLFD"] = dwReq["ABPAUFLFD"];
                dwData["ABAAUFPOS"] = dwReq["ABPAUFPOS"];
                SResult = TMV.VMU.QMU11(dwData);
                TMV.SetProd.Merge(SResult);
                dwData = TMV.VMU.TBMain.NewRow();
                dwData["ABAMANDANT"] = app.Mandant; 
                dwData["ABAVKAUFTYP"] = "V";
                dwData["ABAVKAUFKRS"] = dwReq["ABPAUFKRS"];
                dwData["ABAVKAUFLFD"] = dwReq["ABPAUFLFD"];
                dwData["ABAVKAUFPOS"] = dwReq["ABPAUFPOS"];
                DataTable TBFP = TMV.VMU.QMU21(dwData).Tables[0];
                if (TBFP.Rows.Count > 0)
                    IsFP = true;
                if (!IsAuto)
                    return;
                if (TBMain != null)
                {
                    TBMain.Clear();
                }
                foreach (DataRow dwVK in SResult.Tables[0].Rows)
                {
                    //dwVK已经包含了VKAUF。。。。
                    MergeData(TMV.QGD24(dwVK), true, true);
                    PB.IsExpanded = true;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }



        private void TakeOverZUCommand(object sender, RoutedEventArgs e)
        {

            try
            {
                if(IsFP)
                {
                    throw new Exception("已经分盘，禁止转换!");
                }
                if (dwReq == null)
                {
                    MessageBox.Show("dwReq为空！");
                    return;
                }
                if (dwGD == null)
                {
                    MessageBox.Show("请选中右下角一行再进行转换！");
                    return;
                }
                if (ABPSOLLMENGE7 != double.Parse(dwReq["ABPSOLLMENGE7"].ToString()))
                {
                    throw new Exception("对应销售单数量不等" + ABPSOLLMENGE7 / 100 + "<>" + double.Parse(dwReq["ABPSOLLMENGE7"].ToString()) / 100 + "，通常引起的原因是合投处理不当");
                }
                PM["KRS"] = dwGD["GAAUFKRS"];
                PM["LFD"] = dwGD["GAAUFLFD"];
                PM["POS"] = dwGD["GAAUFPOS"];
                TMV.VZU.LOADZU(PM);
                DataTable TBZU = TMV.VZU.TBMain.Copy();
                dwData = TBZU.Clone().NewRow();
                dwReq["ABPSTKLG"] = ABPSTKLG;
                dwReq["ABPSOLLMENGE5"] = ABPSOLLMENGE5;
                dwReq["ABPSOLLMENGE7"] = ABPSOLLMENGE7;
                TMV.ABP.UABP08(dwReq);
                bool IsFirst = true;
                if (TBZU.Rows.Count == 0)
                {
                    MessageBox.Show("已经转换,禁止再次转换！");
                    return;
                }
                    
                foreach (DataRow dwZU in TBZU.Rows)
                {
                    dwData.ItemArray = dwZU.ItemArray;
                    TMV.VZU.DeleteZUTEI(dwZU, 1);
                    TMV.VGW.NWBBEW(dwZU, "62", RUNID);
                    dwData["ZUAUFKRS"] = dwReq["ABPAUFKRS"];
                    dwData["ZUAUFLFD"] = dwReq["ABPAUFLFD"];
                    dwData["ZUAUFPOS"] = dwReq["ABPAUFPOS"];
                    if (IsFirst)
                    {
                        TMV.VZU.DeleteZUTEI(dwData, 2);
                        IsFirst = false;
                    }
                    TMV.VZU.InsertZUTEI(dwData, app.company);
                    TMV.VGW.NWBBEW(dwData, "61", RUNID);
                }
                IsFirst = true;
                SResult = TMV.QGD29(dwGD);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    row["GAAUFKRS"] = dwReq["ABPAUFKRS"];
                    row["GAAUFLFD"] = dwReq["ABPAUFLFD"];
                    row["GAAUFPOS"] = dwReq["ABPAUFPOS"];
                    if (IsFirst)
                    {
                        TMV.DeleteGAAUF(row, 3);//删除全部GAAUF，然后逐行插入
                        IsFirst = false;
                    }
                    TMV.InsertGAAUF(row, app.company);
                }
                if (TMV.SetProd.Tables.Contains("ABAUFM"))
                {
                    foreach (DataRow dwVKP in TMV.SetProd.Tables["ABAUFM"].Rows)
                    {
                        dwVKP["VKPEXART"] = Helpers.Helper.ConvertGB2312To8859P1("3.已投料");
                        TMV.VVKP.UpdateVKPOS(dwVKP, app.company, 11);
                        PM["RUNID"] = this.RUNID;
                        PM["VKPAUF"] = dwVKP["VKPAUFKRS"].ToString() + dwVKP["VKPAUFLFD"].ToString() + dwVKP["VKPAUFPOS"].ToString();
                        PM["VKPEXART"] = dwVKP["VKPEXART"].ToString();
                        TMV.VVKP.VKPCHK(PM); 
                    }
                }
                MessageBox.Show("转换完成，请执行查询！");
                if (TMV.SetProd.Tables.Contains("ABAUFM"))
                {
                    DataRow dwvkp = TMV.SetProd.Tables["ABAUFM"].Rows[0];
                    PM["CUSER"] = app.User;
                    PM["EVENTID"] = "MYGRABFB09";
                    PM["dwInput"] = dwvkp;
                    PM["KRS"] = dwvkp["VKPAUFKRS"];
                    PM["LFD"] = dwvkp["VKPAUFLFD"];
                    PM["POS"] = dwvkp["VKPAUFPOS"];
                    PM["MSG"] = new MessageInfo();
                    PM["INFO"] = Helpers.Helper.ConvertGB2312To8859P1("转换原料");
                    VICC_WFEVENTS.SVWF.NEWEVENTS(PM);
                    if (dwvkp["VKPART"].ToString().Trim().Substring(0, 2) == "45" & dwvkp["VKPEXAUFPOS"].ToString().Trim() == "010")
                    {
                        MessageBox.Show("请选中11行,转换B纱原料！");
                    }
                }
                 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
